package cn.libinal.scloud.base.controller;

import cn.libinal.scloud.base.common.dto.DtoMapper;
import cn.libinal.scloud.base.common.dto.PermissionDto;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.entity.Permission;
import cn.libinal.scloud.base.service.PermissionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author libinal
 * created at 2018/11/6 20:56
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    private final PermissionService permissionService;
    private final DtoMapper dtoMapper;

    public PermissionController(PermissionService permissionService, DtoMapper dtoMapper) {
        this.permissionService = permissionService;
        this.dtoMapper = dtoMapper;
    }

    @GetMapping(name = "根据角色Id获取权限", path = "getPermissionByRoleId")
    public ResultJson<Set<PermissionDto>> getPermissionByRoleId(@RequestParam("roleId") String roleId) {
        Set<Permission> permissions = permissionService.getRolePermission(roleId);
        Set<PermissionDto> permissionDtos = dtoMapper.mapAsSet(permissions, PermissionDto.class);
        return ResultJson.success(permissionDtos);
    }
}
